package com.cn.xiaonuo.core.dbs;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import com.cn.xiaonuo.core.util.StringUtils;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
public class BeetlSqlDataSourceConfig {

	@Bean(name = "beetlSqlDatasource")
	public DataSource beetlSqlDatasource(Environment env) {
		HikariDataSource ds = new HikariDataSource();
		if(StringUtils.isNotEmpty(env.getProperty("spring.datasource.url"))) {
			ds.setJdbcUrl(env.getProperty("spring.datasource.url"));
		}else {
			ds.setJdbcUrl(env.getProperty("spring.datasource.dynamic.datasource.master.url"));
		}
		if(StringUtils.isNotEmpty(env.getProperty("spring.datasource.username"))) {
			ds.setUsername(env.getProperty("spring.datasource.username"));
		}else {
			ds.setUsername(env.getProperty("spring.datasource.dynamic.datasource.master.username"));
		}
		if(StringUtils.isNotEmpty(env.getProperty("spring.datasource.password"))) {
			ds.setPassword(env.getProperty("spring.datasource.password"));
		}else {
			ds.setPassword(env.getProperty("spring.datasource.dynamic.datasource.master.password"));
		}
		if(StringUtils.isNotEmpty(env.getProperty("spring.datasource.driver-class-name"))) {
			ds.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
		}else {
			ds.setDriverClassName(env.getProperty("spring.datasource.dynamic.datasource.master.driver-class-name"));
		}
		return ds;
	}

}
